/*
 * Copyright 2012 Daniel Kurka
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.mgwt.examples.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.RootPanel;
import com.googlecode.mgwt.mvp.client.Animation;
import com.googlecode.mgwt.ui.client.MGWT;
import com.googlecode.mgwt.ui.client.MGWTSettings;
import com.googlecode.mgwt.ui.client.animation.AnimationHelper;
import com.googlecode.mgwt.ui.client.widget.GroupingCellList;
import com.googlecode.mgwt.ui.client.widget.GroupingCellList.CellGroup;
import com.googlecode.mgwt.ui.client.widget.GroupingCellList.StandardCellGroup;
import com.googlecode.mgwt.ui.client.widget.HeaderList;
import com.googlecode.mgwt.ui.client.widget.LayoutPanel;
import com.googlecode.mgwt.ui.client.widget.celllist.Cell;

public class HeaderListExample implements EntryPoint {

	protected static String[] labels = new String[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#" };

	public class Header {
		private final String name;

		public Header(String name) {
			this.name = name;

		}

		public String getName() {
			return name;
		}

	}

	public class Content {

		private final String name;

		public Content(String name) {
			this.name = name;
		}

		public String getName() {
			return name;
		}

	}

	private static class ContentCell implements Cell<Content> {

		@Override
		public void render(SafeHtmlBuilder safeHtmlBuilder, Content model) {
			safeHtmlBuilder.appendEscaped(model.getName());

		}

		@Override
		public boolean canBeSelected(Content model) {
			return true;
		}

	}

	private static class HeaderCell implements Cell<Header> {

		@Override
		public void render(SafeHtmlBuilder safeHtmlBuilder, Header model) {
			safeHtmlBuilder.appendEscaped(model.getName());
		}

		@Override
		public boolean canBeSelected(Header model) {
			return false;
		}

	}

	@Override
	public void onModuleLoad() {
		//set viewport and other settings for mobile
		MGWT.applySettings(MGWTSettings.getAppSetting());

		//build animation helper and attach it
		AnimationHelper animationHelper = new AnimationHelper();
		RootPanel.get().add(animationHelper);

		//build some UI
		LayoutPanel layoutPanel = new LayoutPanel();
		GroupingCellList<Header, Content> groupingCellList = new GroupingCellList<Header, Content>(new ContentCell(), new HeaderCell());
		HeaderList<Header, Content> headerList = new HeaderList<Header, Content>(groupingCellList);

		layoutPanel.add(headerList);

		//render some data
		headerList.render(buildList());

		//animate
		animationHelper.goTo(layoutPanel, Animation.SLIDE);

	}

	private List<CellGroup<Header, Content>> buildList() {
		ArrayList<CellGroup<Header, Content>> list = new ArrayList<CellGroup<Header, Content>>();

		for (int i = 0; i < labels.length; i++) {
			final Header header = new Header(labels[i]);
			final ArrayList<Content> arrayList = new ArrayList<Content>();

			int max = (int) (Math.random() * 5);

			for (int j = 0; j < max; j++) {
				arrayList.add(new Content("" + j));
			}

			CellGroup<Header, Content> cellGroup = new StandardCellGroup<Header, Content>(header.getName(), header, arrayList);

			list.add(cellGroup);

		}

		return list;
	}

}
